﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;

namespace Ue.Extensions
{
    public static class HtmlExtensions
    {
        //使用正则表达式匹配嵌套html
        //http://www.imkevinyang.com/2009/07/%E4%BD%BF%E7%94%A8%E6%AD%A3%E5%88%99%E8%A1%A8%E8%BE%BE%E5%BC%8F%E5%8C%B9%E9%85%8D%E5%B5%8C%E5%A5%97html%E6%A0%87%E7%AD%BE.html

        public static string FindHtmlById(this string html,string tagId)
        {
            var exp=string.Format("<(?<HtmlTag>[\\w]+)[^>]*\\s[iI][dD]=(?<Quote>[\"']?){0}(?(Quote)\\k<Quote>)[\"']?[^>]*(/>|>((?<Nested><\\k<HtmlTag>[^>]*>)|</\\k<HtmlTag>>(?<-Nested>)|.*?)*</\\k<HtmlTag>>)",tagId);

            var match=  Regex.Match(html,exp, RegexOptions.Singleline);

            return match==null?match.Value:string.Empty;
        }
    }
}
